package com.campusnews.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.campusnews.pojo.Admin;
import com.campusnews.service.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class TokenServiceImpl implements TokenService {

    @Override
    public String getToken(Admin admin) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 4*60* 60 * 1000;//4小时有效时间
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(admin.getAdminId().toString()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(admin.getPassword()));
        return token;
    }
}
